Re: [SQL] Time related question... - Mailing list pgsql-sql

From Herouth Maoz
Subject Re: [SQL] Time related question...
Date
Msg-id l03110705b1ad6d60a3ab@[147.233.159.109]
Whole thread Raw
In response to [SQL] Time related question...  (kcolagio@wc.eso.mc.xerox.com (Kevin Colagio))
List pgsql-sql
At 15:33 +0300 on 17/6/98, Kevin Colagio wrote:


>
> My question is: what is the SQL statement that will allow me to find:
>   1) a list of the calls where the difference between the dateentered and
>   dateclosed is less than (say) 3 days.

Here is an example:

testing=> \d example4

Table    = example4
+--------------------------+------------------------------+-------+
|           Field          |              Type            | Length|
+--------------------------+------------------------------+-------+
| entered                  | datetime                     |     8 |
| closed                   | datetime                     |     8 |
+--------------------------+------------------------------+-------+

testing=> SELECT * FROM example4;
entered                     |closed
----------------------------+----------------------------
Wed Jan 14 00:00:00 1998 IST|Thu Jan 15 00:00:00 1998 IST
Wed Jan 14 00:00:00 1998 IST|Sun Jan 18 00:00:00 1998 IST
Thu Mar 19 23:00:00 1998 IST|Tue Feb 17 00:00:00 1998 IST
Wed Apr 01 00:00:00 1998 IDT|Thu Apr 01 00:00:00 1999 IST
Mon Jun 01 00:00:00 1998 IDT|Wed Jun 03 00:00:00 1998 IDT
(5 rows)

testing=> SELECT *
testing-> FROM example4
testing-> WHERE (closed - entered) < '3 days';
entered                     |closed
----------------------------+----------------------------
Wed Jan 14 00:00:00 1998 IST|Thu Jan 15 00:00:00 1998 IST
Thu Mar 19 23:00:00 1998 IST|Tue Feb 17 00:00:00 1998 IST
Mon Jun 01 00:00:00 1998 IDT|Wed Jun 03 00:00:00 1998 IDT
(3 rows)

If you're wondering why you got the second row, it's because it has an
entered date which is greater than the closed date. A negative difference
is less than 3 days... So you should actually make sure this doesn't
happen, like this:

testing=> SELECT *
testing-> FROM example4
testing-> WHERE closed > entered
testing->   AND (closed - entered) < '3 days';
entered                     |closed
----------------------------+----------------------------
Wed Jan 14 00:00:00 1998 IST|Thu Jan 15 00:00:00 1998 IST
Mon Jun 01 00:00:00 1998 IDT|Wed Jun 03 00:00:00 1998 IDT
(2 rows)

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma



pgsql-sql by date:

Previous
From: Patrice Hédé
Date:
Subject: Re: [SQL] Time related question...
Next
From: "Jose' Soares Da Silva"
Date:
Subject: Re: [SQL] cast text as date